import {
  registerActivity,
  cancelRegistration,
  auditRegistration,
  checkIn,
  getActivityRegistrations,
  getUserRegistrations,
  isUserRegistered,
  getUserRegistration
} from '@/api/registration'

const state = {
  activityRegistrations: [],
  userRegistrations: [],
  pagination: {
    current: 1,
    size: 10,
    total: 0
  },
  registration: null,
  isRegistered: false
}

const mutations = {
  SET_ACTIVITY_REGISTRATIONS: (state, { records, total }) => {
    state.activityRegistrations = records
    state.pagination.total = total
  },
  SET_USER_REGISTRATIONS: (state, { records, total }) => {
    state.userRegistrations = records
    state.pagination.total = total
  },
  SET_PAGINATION: (state, pagination) => {
    state.pagination = { ...state.pagination, ...pagination }
  },
  SET_REGISTRATION: (state, registration) => {
    state.registration = registration
  },
  SET_IS_REGISTERED: (state, isRegistered) => {
    state.isRegistered = isRegistered
  }
}

const actions = {
  // 报名活动
  registerActivity({ commit, dispatch }, activityId) {
    return new Promise((resolve, reject) => {
      registerActivity(activityId).then(response => {
        dispatch('checkUserRegistration', activityId)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 取消报名
  cancelRegistration({ commit, dispatch }, { registrationId, activityId }) {
    return new Promise((resolve, reject) => {
      cancelRegistration(registrationId).then(response => {
        if (activityId) {
          dispatch('checkUserRegistration', activityId)
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 审核报名
  auditRegistration({ commit }, { registrationId, status, remark }) {
    return new Promise((resolve, reject) => {
      auditRegistration(registrationId, status, remark).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 签到
  checkIn({ commit }, registrationId) {
    return new Promise((resolve, reject) => {
      checkIn(registrationId).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取活动报名列表
  getActivityRegistrations({ commit }, { activityId, status, current, size }) {
    return new Promise((resolve, reject) => {
      getActivityRegistrations(activityId, status, current, size).then(response => {
        const { data } = response
        commit('SET_ACTIVITY_REGISTRATIONS', data)
        commit('SET_PAGINATION', { current, size })
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户报名列表
  getUserRegistrations({ commit }, { status, current, size }) {
    return new Promise((resolve, reject) => {
      getUserRegistrations(status, current, size).then(response => {
        const { data } = response
        commit('SET_USER_REGISTRATIONS', data)
        commit('SET_PAGINATION', { current, size })
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 检查用户是否报名活动
  checkUserRegistration({ commit }, activityId) {
    return new Promise((resolve, reject) => {
      isUserRegistered(activityId).then(response => {
        const { data } = response
        commit('SET_IS_REGISTERED', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户报名信息
  getUserRegistration({ commit }, activityId) {
    return new Promise((resolve, reject) => {
      getUserRegistration(activityId).then(response => {
        const { data } = response
        commit('SET_REGISTRATION', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} 